package stack;

import java.util.Arrays;

public class MyStack {
    public int[] elem;
    public int usedSize;
    public MyStack(){
        this.elem = new int[10];
    }
    public void printStack(){
        for (int i = usedSize - 1; i >= 0; i--) {
            System.out.print(elem[i] + " ");
        }
    }
    public void push(int data){
        if(isFull()){
            elem = Arrays.copyOf(elem,2 * elem.length);
        }
        this.elem[usedSize++] = data;
    }
    public boolean isFull(){
        return usedSize == elem.length;
    }
    public boolean isEmpty(){
        return usedSize == 0;
    }
    public int pop(){
        if(isEmpty()){
            throw new StackIndexOutOfException("栈为空，无法出栈");
        } else {
            return elem[--usedSize];
        }
    }
    public int peek(){
        if(isEmpty()){
            throw new StackIndexOutOfException("栈为空");
        } else {
            return elem[usedSize-1];
        }
    }

}
